package io.dbw.common.utils;
import io.dbw.common.enums.ResultStatusEnum;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
　* @description:
　* @author lxc
　* @date 2022/1/7 16:13
*/
@Data
public class Result<T> {

    @ApiModelProperty(value = "返回的编码,000表示成功,其他表示失败" ,position = 1)
    private String code;
    @ApiModelProperty(value = "返回信息" ,position = 2)
    private String msg;
    @ApiModelProperty(value = "返回对象" ,position = 3)
    private T data;


    public Result(String code, String msg, T data){
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public Result(String code, String msg){
        this(code, msg, null);
    }

    public Result(T data) {
        this.code = ResultStatusEnum.SUCCESS.getCode();
        this.msg = ResultStatusEnum.SUCCESS.getMsg();
        this.data = data;
    }
    public Result() {
        this.code = ResultStatusEnum.SUCCESS.getCode();
        this.msg = ResultStatusEnum.SUCCESS.getMsg();
        this.data = null;
    }

    public static <T> Result<T> success(T data){
        Result<T> result =new Result<>();
        result.setData(data);
        result.setCode(ResultStatusEnum.SUCCESS.getCode());
        result.setMsg( ResultStatusEnum.SUCCESS.getMsg());
        return result;
    }
    public static <T> Result<T> success(){
        return success(null);
    }
    public static <T> Result<T> error(String msg){
        Result<T> result =new Result<>();
        result.setCode(ResultStatusEnum.ERROR.getCode());
        result.setMsg(msg);
        return result;
    }
    public static <T> Result<T> error(){
        return error(ResultStatusEnum.ERROR.getMsg());
    }
    public static <T> Result<T> error(T data){
        Result<T> result =new Result<>();
        result.setData(data);
        result.setCode(ResultStatusEnum.ERROR.getCode());
        result.setMsg(ResultStatusEnum.ERROR.getMsg());
        return result;
    }
    public static <T> Result<T> fail(String msg){
        Result<T> result =new Result<>();
        result.setCode(ResultStatusEnum.FAIL.getCode());
        result.setMsg(msg);
        return result;
    }
    public static <T> Result<T> fail(){
        return error(ResultStatusEnum.FAIL.getMsg());
    }
    public static <T> Result<T> info(String code, String msg, T data){
        Result<T> result =new Result<>();
        result.setCode(code);
        result.setMsg(msg);
        if (null!=data){
            result.setData(data);
        }
        return result;
    }
    public static <T> Result<T> info(String code, String msg){
        return info(code,msg,null);
    }

    public static <T> Result<T> info(ResultStatusEnum result){
        return info(result.getCode(),result.getMsg(),null);
    }
}
